﻿using AutoMapper;
using Factory.ProductionManagement.ProcedureSettings.Dto;
using Factory.ProductionManagement.ProcedureSettings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Factory.QualityControl.CommonDefects.Dto;
using Factory.MasterData.Materials.Dto;

namespace Factory.QualityControl.CommonDefects
{
    /// <summary>
    /// 常见缺陷服务
    /// </summary>
    public class CommonDefectAppService : ApplicationService, ICommonDefectAppService
    {
        /// <summary>
        /// 常见缺陷仓储接口
        /// </summary>
        private readonly IRepository<CommonDefect, int> _commonDefectrepository;
        /// <summary>
        /// 映射接口
        /// </summary>
        private readonly IMapper _mapper;
        /// <summary>
        /// 常见缺陷仓储接口
        /// </summary>
        /// <param name="commonDefectrepository">常见缺陷仓储接口</param>
        /// <param name="mapper">映射接口</param>
        public CommonDefectAppService(IRepository<CommonDefect, int> commonDefectrepository, IMapper mapper)
        {
            _commonDefectrepository = commonDefectrepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加常见缺陷信息
        /// </summary>
        /// <param name="commonDefectDto">常见缺陷的数据</param>
        /// <returns>返回结果</returns>
        public async Task<int> Insert(CommonDefectDto commonDefectDto)
        {
            CommonDefect commonDefect = _mapper.Map<CommonDefect>(commonDefectDto);
            await _commonDefectrepository.InsertAsync(commonDefect);
            return 1;
        }
        /// <summary>
        /// 常见缺陷信息分页显示
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="inquireInfo">常见缺陷描述</param>
        /// <returns>返回结果</returns>
        public async Task<PageResult> PageList(int pageIndex, int pageSize, string? inquireInfo)
        {
            var list = await _commonDefectrepository.GetListAsync(m => string.IsNullOrEmpty(inquireInfo) || m.DefectDescription!.Contains(inquireInfo));
            PageResult pageResult = new PageResult();
            pageResult.RowCount = list.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / pageSize);
            pageResult.Queryable = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageResult;
        }
        /// <summary>
        /// 删除常见缺陷信息
        /// </summary>
        /// <param name="id">常见缺陷主键</param>
        /// <returns>返回结果</returns>
        public async Task<int> Deleted(string id)
        {
            var ids = id.Split(',');
            foreach (var item in ids)
            {
                await _commonDefectrepository.DeleteAsync(m => m.Id == Convert.ToInt32(item));
            }
            return 1;
        }
        /// <summary>
        /// 编辑常见缺陷信息
        /// </summary>
        /// <param name="commonDefectDto">编辑的常见缺陷</param>
        /// <returns>返回结果</returns>
        public async Task<int> Update(CommonDefectDto commonDefectDto)
        {
            CommonDefect commonDefect = _mapper.Map<CommonDefect>(commonDefectDto);
            await _commonDefectrepository.UpdateAsync(commonDefect);
            return 1;
        }
    }
}
